package com.dj.msgtable.serviceimpl;


import com.dj.msgtable.dao.UserMapper;
import com.dj.msgtable.entity.User;
import com.dj.msgtable.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper um;
    @Override
    public User searchUser(User user) {
        return um.searchUser(user);
    }

    @Override
    public List<User> searchUserByHigh(User user) {
        return um.searchUserByHigh(user);
    }

    @Override
    public  List<User> searchUserByMiddle(User user) {
        return um.searchUserByMiddle(user);
    }

    @Override
    public User searchUserByName(User user) {
        return um.searchUserByName(user);
    }

    @Override
    public boolean addUserByHigh(User user) {
        return um.addUserByHigh(user);
    }

    @Override
    public boolean addUserByMiddle(User user) {
        return um.addUserByMiddle(user);
    }

    @Override
    public User searchUserById(User user) {
        return um.searchUserById(user);
    }

    @Override
    public String searchLessUser(String[] id) {

//        int  min = (int)Collections.min(Arrays.asList(numbers));
//        int max = (int)Collections.max(Arrays.asList(numbers));
        Integer [] integers = new Integer[id.length];

        for (int i =0;i<id.length;i++){
            User user =new User();
            User user2 =new User();
            user.setId(Integer.valueOf(id[i]));
            user2 = um.searchUserById(user);
            integers[i] = user2.getDailyNum();
        }

        int  min = Collections.min(Arrays.asList(integers));
        if (min<10) {
            for (int i = 0; i < integers.length; i++) {
                if (integers[i] == min) {
                    return id[i];
                }
            }
        }
        return null;
    }

    @Override
    public void addUserDailyNum(User user) {
        um.addUserDailyNum(user);
    }

    @Override
    public void dailyWork() {
        um.dailyWork();
    }
}
